package ace.module.weixin.core.impl.controller.rest.rpc;

import ace.cmp.core.model.R;
import ace.cmp.security.oauth2.resource.server.core.annotation.PreAuthorizeIsInternalClient;
import ace.module.weixin.api.model.dto.WxsGetPhoneNumberResultDto;
import ace.module.weixin.api.model.dto.WxsJsCode2SessionResultDto;
import ace.module.weixin.core.impl.service.WxsMiniAppService;
import ace.module.weixin.sdk.core.api.WxsMiniAppFeign;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Caspar
 */
@AllArgsConstructor
@PreAuthorizeIsInternalClient
@RestController
@RequestMapping(WxsMiniAppFeign.PATH)
@Valid
public class WxsMiniAppRpcController implements WxsMiniAppFeign {
  private final WxsMiniAppService miniAppService;

  @Override
  public R<WxsJsCode2SessionResultDto> getOpenIdByLoginCode(String appId, String loginCode) {
    return R.ok(miniAppService.loginByCode(appId, loginCode));
  }

  @Override
  public R<WxsGetPhoneNumberResultDto> getPhoneNumberByPhoneNumberCode(
      String appId, String phoneNumberCode) {
    WxsGetPhoneNumberResultDto resultDTO =
        this.miniAppService.getPhoneNumberByPhoneNumberCode(appId, phoneNumberCode);
    return R.ok(resultDTO);
  }
}
